package fitdroid.tabs;

import java.util.StringTokenizer;
import coderminus.maps.R;
import fitdroid.helpers.timehelper;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ProgressBar;
import android.widget.TextView;

public class exercisemanager extends Activity{
	Thread thread1;
	private static boolean ignorerep;
	private static boolean ignoredur;
	private static String timeleftstatus; 
	private static String maxduration;
	private static String maxrepetition;
	private ProgressBar timepro;
	private int timeprogressstatus = 0;
	private Handler timeHandler = new Handler();
	
	private Button Backbutton;
	private Button Stopbutton;
	
	private Button Continuebutton;
	private Button Restartbutton;
	private TextView timelefttext;
	private TextView repetitionstext;
	private TextView repetitionsdonetext;
	private static Chronometer timechr;
	private timehelper time= new timehelper();
	
	private OnClickListener BackbuttonListener= new OnClickListener() {
        public void onClick(View v) {
	        	destroy();	            
	        }

		private void destroy() {
			// TODO Auto-generated method stub
			finish();
		}
	    };;
	private OnClickListener StopbuttonListener= new OnClickListener() {
        public void onClick(View v) {
	          	            
	        }
	    };
	private OnClickListener RestartbuttonListener= new OnClickListener() {
        public void onClick(View v) {
	         	            
	        }
	    };
	private OnClickListener ContinuebuttonListener = new OnClickListener() {
        public void onClick(View v) {
	                
	        }
	    };

	public static void init(String repetition, String duration,
			String selectedexercise, boolean ignorerepetition, boolean ignoreduration) {
		// TODO Auto-generated method stub
		ignorerep = ignorerepetition;
		ignoredur = ignoreduration;
		maxduration = duration;
		maxrepetition = repetition;
		timeleftstatus = duration;
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exercisemanager);
		
		//added for exercise 
		Backbutton= (Button)findViewById(R.id.Bbutton);
        Backbutton.setOnClickListener(BackbuttonListener);
        Stopbutton= (Button)findViewById(R.id.Sbutton);
        Stopbutton.setOnClickListener(StopbuttonListener);
        Continuebutton= (Button)findViewById(R.id.Cbutton);
        Continuebutton.setOnClickListener(ContinuebuttonListener);
        Restartbutton= (Button)findViewById(R.id.Sbutton);
        Restartbutton.setOnClickListener(RestartbuttonListener);
        
        
        		
		timepro = (ProgressBar) findViewById(R.id.timepb);
		timechr = (Chronometer) findViewById(R.id.chronometer2);
		
	

		timelefttext = (TextView) findViewById(R.id.timeleft);
		repetitionstext = (TextView) findViewById(R.id.repetitionnumber);
		repetitionsdonetext = (TextView) findViewById(R.id.repetitionsdonenumber);
		
		
		timechr.start();
		
		if (!ignoredur)
		{
		thread1 =	new Thread(new Runnable() {
	            

				public void run() {
	            	
	                while (timeprogressstatus < 100) {
	                	
	                		timeprogressstatus = calctimeprogress();
	                		timeleftstatus = calctimeleftstatus();
	                	
	                	timeHandler.post(new Runnable() {
	                        public void run() {
	                        	
	                        		timepro.setProgress(timeprogressstatus);
	                        		timelefttext.setText(timeleftstatus);
	                        
	                        }
	                    });
	                }
	            }

				private String calctimeleftstatus() {
					// TODO Auto-generated method stub
					return time.calculatedifference(maxduration, timelefttext.getText().toString());
				}
	        });
		thread1.start();
		//.start();
		}

		
	}
	private int calctimeprogress()
	{

		int currenttime = time.converttimeseconds(timechr.getText().toString()); 
		
		StringTokenizer tokenizer = new StringTokenizer (maxduration, ":");
		int calctime=0;
		int factor=60*60;
		
		while (tokenizer.hasMoreTokens())
		{
			calctime += Integer.valueOf(tokenizer.nextToken().trim())*factor;
			factor /= 60;
		}
		//Log.d("current",String.valueOf(currenttime));
		//Log.d("calc",String.valueOf((((currenttime*100)/calctime))));
		if (calctime>0)
			return ((currenttime*100)/calctime);
		else
			return 0;
	}
	
	@Override
    public void onBackPressed ()
    {		
    	super.onBackPressed();
    	TabDemo.tabHost.setCurrentTab(1);
    }
	
	@Override
    public void onPause ()
	{
		SharedPreferences prefs = getSharedPreferences("MapsMinus", Context.MODE_PRIVATE);
    	super.onPause();
    	if (prefs.getBoolean("ReturnMapView", false))
    	{
    		prefs.edit().putBoolean("ReturnMapView", false).commit();
    		//tab_map
    		startActivity(TabDemo.intentlist.get(5));
    	}
    	else
    		TabDemo.tabHost.setCurrentTab(1);
	}
}
